/**
 * 对userId对称加密
 *
 */

const crypto = require('crypto');
const algorithm = 'aes-192-cbc';//aes192，它是 24 个字节（192 位）。
const iv = Buffer.from('a184abf5db891afa', 'utf8'); // 初始化向量。
const authKey = crypto.scryptSync('Dtt123456', 'b7b549f10c408c654ed764c1', 24);

function aesEncrypt(data) {
  //[DEP0106] DeprecationWarning: crypto.createCipher is deprecated.
  const cipher = crypto.createCipheriv(algorithm, authKey, iv);
  var crypted = cipher.update(data, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
}

function aesDecrypt(encrypted) {
  const decipher = crypto.createDecipheriv(algorithm, authKey, iv);
  var decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}

exports.encrypted = aesEncrypt;
exports.decrypted = aesDecrypt;